package com.stadium.controller;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.stadium.common.BaseResponse;
import com.stadium.dto.EventAndScheduleDto;
import com.stadium.dto.GameEventDto;
import com.stadium.dto.GameTeamDto;
import com.stadium.dto.TeamListDto;
import com.stadium.service.GameEventSV;
import com.stadium.vo.GameEventVo;
import com.stadium.vo.searchVo.GameEventSearchVo;
import com.stadium.vo.searchVo.GameTeamSearchVo;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 赛事详情
 *
 * @author wsl
 * @email
 * @date 2021-05-07 20:39:10
 */
@RestController
@RequestMapping("/api/gameevent")
@Api(tags = "赛事详情")
public class GameEventCL{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private GameEventSV gameEventSV;

    @ApiOperation(value = "保存或修改")
    @PostMapping(value = "/v1/save")
    public BaseResponse save(@Validated GameEventVo gameEventVo) {
        try {
            if (gameEventVo.getId() == null ||gameEventVo.getId() == ""){
                    gameEventSV.save(gameEventVo);
            }else{
                    gameEventSV.update(gameEventVo);
            }
            return BaseResponse.ok();

        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/v1/delete")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse delete(@RequestParam(name = "id", required = true)String id) {
        try {
            gameEventSV.delete(id);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }



    @ApiOperation(value = "获得列表")
    @GetMapping(value = "/v1/query")
    public BaseResponse<List<GameEventDto>> query(@Validated GameEventSearchVo searchVo) {
        try {
            if (StringUtils.isNotEmpty(searchVo.getSearchValue())) {
                searchVo.setSearchValue("%" + searchVo.getSearchValue() + "%");
            }
            if (StringUtils.isNotEmpty(searchVo.getArea())) {
                searchVo.setArea(searchVo.getArea() + "%");
            }

            if (searchVo.getIsNeedschedule() == null) {
                searchVo.setIsNeedschedule("0");
            }
            if (searchVo.getIsNeedteam() == null) {
                searchVo.setIsNeedteam("0");
            }
            if (StringUtils.isNotEmpty(searchVo.getSearchValue())) {
                searchVo.setSearchValue("%" + searchVo.getSearchValue() + "%");
            }
            List<GameEventDto> queryList = gameEventSV.query(searchVo);
            int total = gameEventSV.getCount(searchVo);
            return BaseResponse.ok(queryList, total);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得详情", notes = "获得详情")
    @GetMapping(value = "/v1/get")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse<GameEventDto> get(@RequestParam(name = "id", required = true)String id) {
        try {
                GameEventDto queryData = gameEventSV.get(id);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "队伍审批列表")
    @GetMapping(value = "/v1/auditTeam")
    public BaseResponse<List<GameTeamDto>> auditTeam(@Validated GameTeamSearchVo searchVo) {
        try {

            List<GameTeamDto> queryList = gameEventSV.auditTeam(searchVo);
            int total = gameEventSV.auditTeamCount(searchVo);
            return BaseResponse.ok(queryList, total);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "审批")
    @PostMapping(value = "/v1/audit")
    @ApiImplicitParams({@ApiImplicitParam(name="rid",value="rid"),
            @ApiImplicitParam(name="status",value="审核状态（2：未审核 1：审核通过 0：审核未通过）")})
    public BaseResponse audit(@RequestParam(name = "rid", required = true)String rid,
                              @RequestParam(name = "status", required = true)String status) {
        try {
            gameEventSV.audit(rid,status);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "已报名球队列表", notes = "已报名球队列表")
    @GetMapping(value = "/v1/getTeamsByEventId")
    @ApiImplicitParams({@ApiImplicitParam(name="eventId",value="eventId")})
    public BaseResponse<List<TeamListDto>> getTeamsByEventId(@RequestParam(name = "eventId", required = true)String eventId) {
        try {
            List<TeamListDto> queryData = gameEventSV.getTeamsByEventId(eventId);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得球员下球队的比赛列表")
    @GetMapping(value = "/v1/queryByUserId")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "userId")})
    public BaseResponse<List<EventAndScheduleDto>> queryByPlayerUserId(@RequestParam(value = "userId")String userId) {
        try {
            List<EventAndScheduleDto> queryList = gameEventSV.queryByPlayerUserId(userId);
            return BaseResponse.ok(queryList);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }
}
